<?php

require_once('../config/config.php');
require_once('../config/error_handler.php');

// Chat class that contains server-side profiel
class Profiel {

    // database handler
    private $mMysqli;

    // constructor opens database connection
    function __construct() {
        // connect to the database
        $this->mMysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
    }

    // destructor closes database connection
    public function __destruct() {
        $this->mMysqli->close();
    }

    //The postMessages method inserts a profile into the database
    public function postMessage($instellingNaam, $relatienummer, $adres, $postcode, $woonplaats, $province, $telefoon, $email, $wachtwoord, $pakket) {

        // escape the variable data for safely adding them to the database
        $instellingNaam = $this->mMysqli->real_escape_string($instellingNaam);
        $relatienummer = $this->mMysqli->real_escape_string($relatienummer);
        $adres = $this->mMysqli->real_escape_string($adres);
        $postcode = $this->mMysqli->real_escape_string($postcode);
        $woonplaats = $this->mMysqli->real_escape_string($woonplaats);
        $province = $this->mMysqli->real_escape_string($province);
        $telefoon = $this->mMysqli->real_escape_string($telefoon);
        $email = $this->mMysqli->real_escape_string($email);
        $wachtwoord = $this->mMysqli->real_escape_string($wachtwoord);
        $pakket = $this->mMysqli->real_escape_string($pakket);
        $querycheck = "SELECT email FROM werkgeverprofiel WHERE email ='$email'";
        $querychecks = "SELECT email FROM studentprofiel WHERE email ='$email'";

        $resultcheck = $this->mMysqli->query($querycheck);
        $resultchecks = $this->mMysqli->query($querychecks);

        if (mysqli_num_rows($resultcheck) == 0 && mysqli_num_rows($resultchecks) == 0) {
            $query = 'INSERT INTO werkgeverprofiel(instellingnaam, relatienummer, adres, postcode, woonplaats, provencie, telefoon, email, wachtwoord, pakket ) ' .
                    'VALUES ("' . $instellingNaam . '","' . $relatienummer . '","' . $adres . '","' . $postcode . '","' . $woonplaats . '","' . $province . '","' . $telefoon . '","' . $email . '", SHA("' . $wachtwoord . '"),"' . $pakket . '")';
            // execute the SQL query
            $result = $this->mMysqli->query($query);

            $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?p=registratiegelukt';
            header('Location: ' . $home_url);
        } else {
            // An account already exists for this username
            $home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/index.php?p=existaccount';
            header('Location: ' . $home_url);
        }
        $resultcheck->close();
        $resultchecks->close();
    }

}

?>
